#include "sdInput.h"

sdInput* sdInput::this_;

//Construction
sdInput::sdInput(int width, int height) : width_(width), height_(height) {
    for(int i = 0; i < 256; i++) {
        keyDown_[i] = 0;
    }
	
	leftDown_ = false;
	rightDown_ = false;

    this_ = this;
}

//Instance Retreival
sdInput* sdInput::getThis() {
    return this_;
}

//Callbacks
void sdInput::keyboard(unsigned char key, int x, int y) {
    keyDown_[key] = 1;
}

void sdInput::keyboardUp(unsigned char key, int x, int y) {
    keyDown_[key] = 0;
}

void sdInput::mouse(int x, int y) {
    mouseLoc_.x = x - (width_ / 2);
    mouseLoc_.y = (height_ / 2) - y;
}

void sdInput::mouseClick(int button, int state, int x, int y) {
	switch(button) {
	case GLUT_LEFT_BUTTON:
		if(state == GLUT_DOWN) {
			leftDown_ = true;
		} else {
			leftDown_ = false;
		}
		break;

	case GLUT_RIGHT_BUTTON:
		if(state == GLUT_DOWN) {
			rightDown_ = true;
		} else {
			rightDown_ = false;
		}
		break;
	}

	mouseLoc_.x = x - (width_ / 2);
    mouseLoc_.y = (height_ / 2) - y;
}

void sdInput::init() {
    //glutKeyboardFunc(keyboard);
    //glutKeyboardUpFunc(keyboardUp);
    //glutPassiveMotionFunc(mouse);
    //glutMouseFunc(buttons);
}

//Input retreival
bool sdInput::keyDown(int index) {
    if((index < 256) && (index >= 0)) {
        return keyDown_[index] == 1;
    }
    return false;
}

bool sdInput::operator[](char key) {
    int index = (int) key;
    if((index < 256) && (index >= 0)) {
        return keyDown_[index] == 1;
    }
    return false;
}

cpVect sdInput::getDirection() {
    cpVect direction;

    if(keyDown('a') && keyDown('d')) {
        direction.x = 0;
    } else if(keyDown('a')) {
        direction.x = -1.0;
    } else if(keyDown('d')) {
        direction.x = 1.0;
    } else {
        direction.x = 0;
    }

    if(keyDown('w') && keyDown('s')) {
        direction.y = 0;
    } else if(keyDown('w')) {
        direction.y = 1.0;
    } else if(keyDown('s')) {
        direction.y = -1.0;
    } else {
        direction.y = 0;
    }
    return direction;
}

